<template>
  <div class="topology-container" @contextmenu.prevent>
    <!-- 顶部工具栏 -->
    <div class="top-toolbar">
      <div class="toolbar-left">
        <el-button-group>
          <el-button 
            :type="currentMode === 'select' ? 'primary' : 'default'" 
            @click="setMode('select')"
            size="small"
          >
            <el-icon><Mouse /></el-icon>
            选择
          </el-button>
          <el-button 
            :type="currentMode === 'add' ? 'primary' : 'default'" 
            @click="setMode('add')"
            size="small"
          >
            <el-icon><Plus /></el-icon>
            添加
          </el-button>
          <el-button 
            :type="currentMode === 'connect' ? 'primary' : 'default'" 
            @click="setMode('connect')"
            size="small"
          >
            <el-icon><Link /></el-icon>
            连接
          </el-button>
        </el-button-group>
      </div>
      
      <div class="toolbar-center">
        <span class="mode-indicator">当前模式: {{ currentMode }}</span>
      </div>
      
      <div class="toolbar-right">
        <el-button size="small" @click="clearAll">
          <el-icon><Brush /></el-icon>
          清空
        </el-button>
        <el-button size="small" @click="exportJson">
          <el-icon><Download /></el-icon>
          导出
        </el-button>
        <el-button size="small" @click="importJson">
          <el-icon><Upload /></el-icon>
          导入
        </el-button>
      </div>
    </div>

    <!-- 主画布区域 -->
    <div class="canvas-wrapper">
      <div ref="cyRef" class="cytoscape-container"></div>
      
      <!-- 画布覆盖层信息 -->
      <div class="canvas-overlay">
        <div class="stats-overlay">
          节点: {{ nodeCount }} | 连接: {{ edgeCount }}
        </div>
      </div>
    </div>

    <!-- 底部状态栏 -->
    <div class="bottom-status">
      <div class="status-left">
        <el-icon><InfoFilled /></el-icon>
        <span>就绪 - 右键点击画布或节点查看更多操作</span>
      </div>
      <div class="status-right">
        <span>最后更新: {{ lastUpdateTime }}</span>
      </div>
    </div>

    <!-- 侧边信息面板 -->
    <div class="side-panel" :class="{ 'panel-visible': selectedNode }">
      <div class="panel-content">
        <h3>节点信息</h3>
        <div class="node-info" v-if="selectedNode">
          <div class="info-item">
            <label>ID:</label>
            <span>{{ selectedNode.data.id }}</span>
          </div>
          <div class="info-item">
            <label>标签:</label>
            <span>{{ selectedNode.data.label }}</span>
          </div>
          <div class="info-item">
            <label>位置:</label>
            <span>
              X: {{ selectedNode.position.x.toFixed(0) }}, 
              Y: {{ selectedNode.position.y.toFixed(0) }}
            </span>
          </div>
        </div>
        <div class="panel-actions" v-if="selectedNode">
          <el-button size="small" @click="deleteNode(selectedNode.data.id)">
            删除节点
          </el-button>
        </div>
      </div>
    </div>

    <!-- 右键菜单 -->
    <div
      v-if="contextMenu.visible"
      class="context-menu"
      :style="{ top: contextMenu.y + 'px', left: contextMenu.x + 'px' }"
    >
    </div>

    <input
      ref="fileInput"
      type="file"
      accept="application/json"
      style="display: none"
      @change="handleFileChange"
    />
  </div>
</template>

<script setup>
import { ref, onMounted, onUnmounted } from "vue";
import cytoscape from "cytoscape";

const nodeCount = ref(0);
const edgeCount = ref(0);
const selectedNode = ref(null);
const lastUpdateTime = ref(new Date().toLocaleTimeString());

function updateStatistics() {
  if (!cy) return;
  nodeCount.value = cy.nodes().length;
  edgeCount.value = cy.edges().length;
  lastUpdateTime.value = new Date().toLocaleTimeString();
}

cy.on('add remove', () => {
  updateStatistics();
});

cy.on("tap", "node", (evt) => {
  const node = evt.target;
  if (currentMode.value === "select") {
    selectedNode.value = node.json();
    emits("node-selected", node.json());
  }
});

</script>

<style scoped>
.top-toolbar {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 12px 16px;
  background: white;
  border-bottom: 1px solid #e0e0e0;
  box-shadow: 0 2px 4px rgba(0,0,0,0.1);
}

.toolbar-left,
.toolbar-right {
  display: flex;
  gap: 8px;
}

.toolbar-center {
  flex: 1;
  text-align: center;
}

.mode-indicator {
  font-size: 14px;
  color: #666;
  font-weight: 500;
}

.canvas-wrapper {
  position: relative;
  flex: 1;
  background: #f8f9fa;
}

.canvas-overlay {
  position: absolute;
  top: 12px;
  right: 12px;
  pointer-events: none;
}

.stats-overlay {
  padding: 6px 12px;
  background: rgba(0,0,0,0.7);
  color: white;
  border-radius: 6px;
  font-size: 12px;
}

.bottom-status {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 8px 16px;
  background: white;
  border-top: 1px solid #e0e0e0;
  font-size: 12px;
  color: #666;
}

.side-panel {
  position: absolute;
  top: 0;
  right: -300px;
  width: 280px;
  height: 100%;
  background: white;
  box-shadow: -2px 0 8px rgba(0,0,0,0.1);
  transition: right 0.3s ease;
  z-index: 1000;
}

.side-panel.panel-visible {
  right: 0;
}

.panel-content {
  padding: 20px;
}

.panel-content h3 {
  margin: 0 0 16px 0;
  font-size: 16px;
  color: #333;
  border-bottom: 1px solid #e0e0e0;
  padding-bottom: 8px;
}

.node-info {
  display: flex;
  flex-direction: column;
  gap: 12px;
  margin-bottom: 16px;
}

.info-item {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 8px 0;
  border-bottom: 1px solid #f0f0f0;
}

.info-item label {
  font-weight: 500;
  color: #666;
  font-size: 13px;
}

.info-item span {
  font-size: 13px;
  color: #333;
  font-family: monospace;
}

.panel-actions {
  display: flex;
  gap: 8px;
}

@media (max-width: 768px) {
  .top-toolbar {
    flex-direction: column;
    gap: 12px;
  }
  
  .side-panel {
    width: 100%;
    right: -100%;
  }
}
</style>